package edu.up.taglibs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import edu.up.controle.Selectable;

public class MySelectTag extends SimpleTagSupport 
{
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public void doTag() throws JspException, IOException 
	{
		PageContext pageContext = (PageContext)getJspContext();
		try {
			JspWriter out = pageContext.getOut();
			out.print( "<SELECT name='" );
			out.print( getName() );
			out.println( "'>" );			
			ArrayList array = (ArrayList)pageContext.getRequest().getAttribute( getName() );
			Iterator itr = array.iterator();
			while (itr.hasNext()) {
				String object = (String) itr.next();
				out.print( "<OPTION value='" );
				out.print( object );
				out.print( "'>" );
				out.print( object );
				out.println( "</OPTION>" );	
			}
			out.println( "</SELECT>" );			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
